package com.example.demo.config;

import com.example.demo.ws.TestWebService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * webservice的cxf配置。参考自：https://www.jianshu.com/p/354522360e21
 *
 * @author TongDaShuai
 * @since 2021/10/27
 */
@Configuration
public class CxfConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private TestWebService testWebService;

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, testWebService);
        endpoint.publish("/testWebService");
        return endpoint;
    }

    /**
     * 这里重写注册了下CXFServlet，为了修改访问的拦截路径，默认拦截的是services,这里改成了webservice
     * @return
     */
    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
    }
}
